import router from '@/router'   // 你的 router 实例
import { useUserStore } from '@/stores/useUser'
import { storeToRefs } from 'pinia'

export function setupPermission() {
  router.beforeEach((to:any, from:any, next:any) => {
    const { checkedKeys, adminname } = storeToRefs(useUserStore())

    // 1. 白名单，直接放行
    const whiteList = ['/login', '/np', '/404']
    if (whiteList.includes(to.path)) return next()

    // 2. admin 拥有全部权限
    if (adminname.value === 'admin') return next()

    // 3. 无权限列表或当前路径不在列表里 → 跳到 /np
    if (!checkedKeys.value.length || !checkedKeys.value.includes(to.path)) {
      return next('/np')   // 跳转无权限提示页
    }

    // 4. 其余正常放行
    next()
  })
}